home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Sound / LAME / WarpOS / src / mpglib / layer1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-05  |  4.0 KB  |  171 lines

  1. /* 
  2.  * Mpeg Layer-1 audio decoder 
  3.  * --------------------------
  4.  * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
  5.  * near unoptimzed ...
  6.  *
  7.  * may have a few bugs after last optimization ... 
  8.  *
  9.  */
  10.  
  11. /* $Id: layer1.c,v 1.16 2001/01/05 15:20:34 aleidinger Exp $ */
  12.  
  13. #ifdef HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16.  
  17. #ifdef USE_LAYER_1
  18.  
  19. #include <assert.h>
  20. #include "common.h"
  21. #include "decode_i386.h"
  22.  
  23. #ifdef WITH_DMALLOC
  24. #include <dmalloc.h>
  25. #endif
  26.  
  27. void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],struct frame *fr)
  28. {
  29.   unsigned int *ba=balloc;
  30.   unsigned int *sca = (unsigned int *) scale_index;
  31.  
  32.   assert ( fr->stereo == 0 || fr->stereo == 1 );
  33.   if(fr->stereo) {
  34.     int i;
  35.     int jsbound = fr->jsbound;
  36.     for (i=0;i<jsbound;i++) { 
  37.       *ba++ = getbits(4);
  38.       *ba++ = getbits(4);
  39.     }
  40.     for (i=jsbound;i<SBLIMIT;i++)
  41.       *ba++ = getbits(4);
  42.  
  43.     ba = balloc;
  44.  
  45.     for (i=0;i<jsbound;i++) {
  46.       if ((*ba++))
  47.         *sca++ = getbits(6);
  48.       if ((*ba++))
  49.         *sca++ = getbits(6);
  50.     }
  51.     for (i=jsbound;i<SBLIMIT;i++)
  52.       if ((*ba++)) {
  53.         *sca++ =  getbits(6);
  54.         *sca++ =  getbits(6);
  55.       }
  56.   }
  57.   else {
  58.     int i;
  59.     for (i=0;i<SBLIMIT;i++)
  60.       *ba++ = getbits(4);
  61.     ba = balloc;
  62.     for (i=0;i<SBLIMIT;i++)
  63.       if ((*ba++))
  64.         *sca++ = getbits(6);
  65.   }
  66. }
  67.  
  68. void I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT],
  69.     unsigned int scale_index[2][SBLIMIT],struct frame *fr)
  70. {
  71.   int i,n;
  72.   int smpb[2*SBLIMIT]; /* values: 0-65535 */
  73.   int *sample;
  74.   register unsigned int *ba;
  75.   register unsigned int *sca = (unsigned int *) scale_index;
  76.  
  77.   assert ( fr->stereo == 0 || fr->stereo == 1 );
  78.   if(fr->stereo) {
  79.     int jsbound = fr->jsbound;
  80.     register real *f0 = fraction[0];
  81.     register real *f1 = fraction[1];
  82.     ba = balloc;
  83.     for (sample=smpb,i=0;i<jsbound;i++)  {
  84.       if ((n = *ba++))
  85.         *sample++ = getbits(n+1);
  86.       if ((n = *ba++))
  87.         *sample++ = getbits(n+1);
  88.     }
  89.     for (i=jsbound;i<SBLIMIT;i++) 
  90.       if ((n = *ba++))
  91.         *sample++ = getbits(n+1);
  92.  
  93.     ba = balloc;
  94.     for (sample=smpb,i=0;i<jsbound;i++) {
  95.       if((n=*ba++))
  96.         *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  97.       else
  98.         *f0++ = 0.0;
  99.       if((n=*ba++))
  100.         *f1++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  101.       else
  102.         *f1++ = 0.0;
  103.     }
  104.     for (i=jsbound;i<SBLIMIT;i++) {
  105.       if ((n=*ba++)) {
  106.         real samp = (real)( ((-1)<<n) + (*sample++) + 1);
  107.         *f0++ = samp * muls[n+1][*sca++];
  108.         *f1++ = samp * muls[n+1][*sca++];
  109.       }
  110.       else
  111.         *f0++ = *f1++ = 0.0;
  112.     }
  113.     for(i=fr->down_sample_sblimit;i<32;i++)
  114.       fraction[0][i] = fraction[1][i] = 0.0;
  115.   }
  116.   else {
  117.     register real *f0 = fraction[0];
  118.     ba = balloc;
  119.     for (sample=smpb,i=0;i<SBLIMIT;i++)
  120.       if ((n = *ba++))
  121.         *sample++ = getbits(n+1);
  122.     ba = balloc;
  123.     for (sample=smpb,i=0;i<SBLIMIT;i++) {
  124.       if((n=*ba++))
  125.         *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  126.       else
  127.         *f0++ = 0.0;
  128.     }
  129.     for(i=fr->down_sample_sblimit;i<32;i++)
  130.       fraction[0][i] = 0.0;
  131.   }
  132. }
  133.  
  134. //int do_layer1(struct frame *fr,int outmode,struct audio_info_struct *ai)
  135. int do_layer1(PMPSTR mp, unsigned char *pcm_sample,int *pcm_point)
  136. {
  137.   int clip=0;
  138.   unsigned int balloc[2*SBLIMIT];
  139.   unsigned int scale_index[2][SBLIMIT];
  140.   real fraction[2][SBLIMIT];
  141.   struct frame *fr=&(mp->fr);
  142.   int i,stereo = fr->stereo;
  143.   int single = fr->single;
  144.  
  145.   fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
  146.  
  147.   if(stereo == 1 || single == 3)
  148.     single = 0;
  149.  
  150.   I_step_one(balloc,scale_index,fr);
  151.  
  152.   for (i=0;i<SCALE_BLOCK;i++)
  153.   {
  154.     I_step_two(fraction,balloc,scale_index,fr);
  155.  
  156.     if(single >= 0)
  157.     {
  158.       clip += synth_1to1_mono( mp, (real *) fraction[single],pcm_sample,pcm_point);
  159.     }
  160.     else {
  161.         int p1 = *pcm_point;
  162.         clip += synth_1to1( mp, (real *) fraction[0],0,pcm_sample,&p1);
  163.         clip += synth_1to1( mp, (real *) fraction[1],1,pcm_sample,pcm_point);
  164.     }
  165.   }
  166.  
  167.   return clip;
  168. }
  169.  
  170. #endif
  171.